package com.dc.datacollect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.datacollect.dto.SysUserDTO;
import com.dc.datacollect.dto.input.RenameGroupsInput;
import com.dc.datacollect.entity.Groups;
import com.dc.datacollect.mapper.GroupsMapper;
import com.dc.datacollect.service.GroupItemService;
import com.dc.datacollect.service.GroupsService;
import com.dc.datacollect.helpers.SessionProvide;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dc.datacollect.entity.GroupItem;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author zhuangjie
* @description 针对表【groups(收集集合)】的数据库操作Service实现
* @createDate 2023-06-19 14:50:58
*/
@Service
public class GroupsServiceImpl extends ServiceImpl<GroupsMapper, Groups>
    implements GroupsService {
    @Autowired
    private GroupItemService groupItemService;

    @Override
    public boolean createGroups(String groupsName) throws Exception {
        SysUserDTO loginObject = SessionProvide.getLoginObject();
        // 先查看是否已经存在此名称的集合
        LambdaQueryWrapper<Groups> groupsQueryWrapper = new LambdaQueryWrapper<>();
        groupsQueryWrapper.eq(Groups::getCollectorId,loginObject.getId())
                .eq(Groups::getName,groupsName);
        Groups groups = this.getOne(groupsQueryWrapper);
        if (groups != null ) {
            throw new Exception("集合已经存在");
        }
        // 当前用户没有此名称的集合,创建
        Groups newGroups = new Groups();
        newGroups.setName(groupsName);
        newGroups.setCollectorId(loginObject.getId());
        return this.save(newGroups);
    }

    @Override
    public List<Groups> getGroupsTreeByCollectorId(Long collectorId) {
        // 查询出当前collector的所有集合
        LambdaQueryWrapper<Groups> groupsQueryWrapper = new LambdaQueryWrapper<>();
        groupsQueryWrapper.eq(Groups::getCollectorId,collectorId);
        List<Groups> groupslist = this.list(groupsQueryWrapper);
        List<Long> groupsIds = groupslist.stream().map(groups -> groups.getId()).collect(Collectors.toList());
        Map<Long, Groups> groupsMap = groupslist.stream().collect(Collectors.toMap(groups -> groups.getId(), groups -> groups));
        // 查询出当前collector的所有收集组
        LambdaQueryWrapper<GroupItem> groupQueryWrapper = new LambdaQueryWrapper<>();
        groupQueryWrapper.in(GroupItem::getGroupsId,groupsIds);
        // 将groupList组装到对应的groups
        List<GroupItem> groupList = groupItemService.list(groupQueryWrapper);
        groupList.forEach(groupItem -> {
            Long groupsId = groupItem.getGroupsId();
            Groups parentGroups = groupsMap.get(groupsId);
            if (parentGroups.getGroupList() == null) {
                parentGroups.setGroupList(new ArrayList<>());
            }
            parentGroups.getGroupList().add(groupItem);
        });
        List<Groups> groupsTree = groupsMap.values().stream().collect(Collectors.toList());
        return groupsTree;
    }

    @Override
    @Transactional
    public boolean removeGroupsById(Long groupsId) {
        // 删除集合下的收集组
        LambdaQueryWrapper<GroupItem> groupItemQueryWrapper = new LambdaQueryWrapper<>();
        groupItemQueryWrapper.eq(GroupItem::getGroupsId,groupsId);
        groupItemService.remove(groupItemQueryWrapper);
        // 删除集合
        return this.removeById(groupsId);
    }

    @Override
    public Boolean rename(RenameGroupsInput renameGroupsInput) throws Exception {
        // 查出组集合
        Groups groups = this.getById(renameGroupsInput.getGroupsId());
        if (groups == null) {
            throw new Exception("组集合不存在");
        }
        groups.setName(renameGroupsInput.getNewName());
        return this.updateById(groups);
    }
}




